package cl.interpreter.statements;

import java.util.List;

import cl.interpreter.DuplicateVariableException;
import cl.interpreter.Environment;
import cl.interpreter.IncorrectTypeException;
import cl.interpreter.NonExistantVariableException;
import cl.interpreter.RuntimeStack;
import cl.interpreter.Statement;
import cl.interpreter.Variable;

public class Function implements Statement {
    
    private List<Variable> args;
    private List<Statement> statements;
    
    public Function(List<Variable> args, List<Statement> statements) {
        this.args = args;
        this.statements = statements;
    }
    
    @Override
    public void execute(RuntimeStack stack, Environment parent) throws DuplicateVariableException, NonExistantVariableException, IncorrectTypeException {
        Environment child = new Environment(parent);
        
        for(Variable v : args) {
            child.addVariable(v);
        }
        
        stack.call(args, 0);
        
        for(Statement s: statements) {
            s.execute(stack, child);
        }
            
        stack.leave();
    }
}
